package cn.lg.soar.cms.biz.modules.other.service.impl;

import cn.lg.soar.cms.biz.config.CmsCacheKey;
import cn.lg.soar.cms.biz.modules.other.entity.Block;
import cn.lg.soar.cms.biz.modules.other.mapper.BlockMapper;
import cn.lg.soar.cms.biz.modules.other.model.BlockItemVo;
import cn.lg.soar.cms.biz.modules.other.model.BlockVO;
import cn.lg.soar.cms.biz.modules.other.service.BlockItemService;
import cn.lg.soar.cms.biz.modules.other.service.BlockService;
import cn.lg.soar.common.util.AssertUtil;
import cn.lg.soar.database.base.service.impl.BaseServiceImpl;
import cn.lg.soar.system.api.service.IFileApi;
import cn.lg.soar.system.api.utils.BizFileBuilder;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.List;

/**
 * 页面元素位置
 * @author luguoxiang
 * @since 2025-02-23
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Service
public class BlockServiceImpl extends BaseServiceImpl<BlockMapper, Block> implements BlockService {


    @Autowired
    private BlockItemService itemService;

    @Cacheable(cacheNames = CmsCacheKey.BLOCK)
    @Override
    public Block getByCode(String code) {
        return getOne(
                Wrappers.<Block>lambdaQuery().eq(Block::getCode, code)
        );
    }

    @Override
    public List<BlockItemVo> contents(String code, Integer pageSize) {
        Block position = getByCode(code);
        AssertUtil.notNull(position, "位置不存在");
        return itemService.voListByBlockId(position.getId(), pageSize);
    }

    @CacheEvict(cacheNames = CmsCacheKey.BLOCK, key = "#p0.code")
    @Override
    public boolean updateById(Block entity) {
        return super.updateById(entity);
    }

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void remove(Collection<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return;
        }
        itemService.removeByBlockIds(ids);
        AssertUtil.isTrue(super.removeByIds(ids), "删除失败");
    }

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public Long create(BlockVO data) {
        AssertUtil.isTrue(save(data), "操作失败");
        // 保存附件信息
        boolean saveFileFlag = BizFileBuilder.writer(data.getId(), data).execute();
        AssertUtil.isTrue(saveFileFlag, "保存附件失败");
        return data.getId();
    }

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void update(BlockVO data) {
        AssertUtil.isTrue(updateById(data), "操作失败");
        // 保存附件信息
        boolean saveFileFlag = BizFileBuilder.writer(data.getId(), data).execute();
        AssertUtil.isTrue(saveFileFlag, "保存附件失败");
    }

    @Override
    public BlockVO detail(Long id) {
        Block data = getById(id);
        BlockVO vo = BlockVO.from(data);
        // 获取附件信息
        BizFileBuilder.reader(id, vo).execute();
        return vo;
    }

}
